﻿/*
 * Created by SharpDevelop.
 * User: Lousabit
 * Date: 01/11/2011
 * Time: 15:29
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using DAOFactory;
using Business_Entity;

namespace DAOFactory
{
	/// <summary>
	/// Description of DAOHelper.
	/// </summary>
	public class DAOHelper
	{
		SqlCommand cmd;
		List<int> outs;
		List<String> pars;
		List<String> tipos;
		List<object> valoresOut;
		
		public DAOHelper(String cadena)
		{
			valoresOut = new List<object>();
			outs = new List<int>();
			pars = new List<String>();
			tipos = new List<String>();
			
			String[] lineas = cadena.Split('\n');
			String procedure = lineas[0].Substring(0, lineas[0].Length-1);
			cmd = new SqlCommand(procedure, ConnectionManager.getConnection());
			cmd.CommandType = CommandType.StoredProcedure;
			
			for (int i=1; i<lineas.Length; i++){
				String[] pals = lineas[i].Split(' ');
				String parametro = quitarEspacios(pals[0]);
				String tipo = quitarEspacios(pals[1].ToUpper());
				bool esOut = false;
				if (pals.Length == 3 && pals[2].ToUpper().IndexOf("OUTPUT") >=0) esOut = true;
				
				SqlDbType tipoSql = SqlDbType.Int;
				
				//MessageBox.Show("TIPO: "+tipo);
				if (tipo.IndexOf("DATETIME") >= 0){
					tipoSql = SqlDbType.DateTime;
				} else if (tipo.IndexOf("DATE")>= 0){
					tipoSql = SqlDbType.Date;
				}else if (tipo.IndexOf("INT") >= 0){
					tipoSql = SqlDbType.Int;
				}else if (tipo.IndexOf("NVARCHAR") >= 0){
					tipoSql = SqlDbType.NVarChar;
				}else if(tipo.IndexOf("FLOAT") >= 0){
					tipoSql = SqlDbType.Float;
				}
								
				pars.Add(parametro);
				tipos.Add(tipo);
				
				if (esOut){
					outs.Add(i-1);
					cmd.Parameters.Add(parametro, tipoSql);
					cmd.Parameters[parametro].Direction = ParameterDirection.Output;
				}else{
					cmd.Parameters.Add(parametro, tipoSql);
				}
				
			}
		}
			
		public void ExecuteNonQuery(params object[] p){
			for (int i=0; i<pars.Count; i++){
				if (!outs.Contains(i)){
					//es input
					
					object valor = p[i];
					if (tipos[i].IndexOf("DATE") >= 0){
						valor = ((DateTime)(p[i])).ToShortDateString();
					}
					cmd.Parameters[pars[i]].Value = valor;
					
					//MessageBox.Show(tipos[i]+" - "+pars[i]+" : "+cmd.Parameters[pars[i]].Value);
				}
			}
			
			cmd.ExecuteNonQuery();
			
			for (int i=0; i<outs.Count; i++){
				int ind = outs[i];
				valoresOut.Add(cmd.Parameters[pars[ind]].Value);
			}
						
		}
		
		public SqlDataReader ExecuteReader(params object[] p){
			for (int i=0; i<pars.Count; i++){
				if (!outs.Contains(i)){
					//es input
					
					object valor = p[i];
					if (tipos[i].IndexOf("DATE") >= 0){
						valor = ((DateTime)(p[i])).ToShortDateString();
					}
					cmd.Parameters[pars[i]].Value = valor;
					
					//MessageBox.Show(tipos[i]+" - "+pars[i]+" : "+cmd.Parameters[pars[i]].Value);
				}
			}
			
			return cmd.ExecuteReader();
		}
		
		public object getOutput(String id){
			//retorna valor out
			int ind = pars.IndexOf(id);
			if (ind == -1) return null;
			int indOut = outs.IndexOf(ind);
			if(indOut == -1) return null;
			return valoresOut[indOut];
		}
		
		public static String quitarEspacios(String str){
			String nstr = "";
			for(int i=0; i<str.Length; i++){
				if (!(str[i].Equals(' ') || str[i].Equals('\t'))){
					nstr += str[i];
				}
			}
			return nstr;
		}
		
	}
}
